package com.cqan.message.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import com.cqan.account.entity.User;
import com.cqan.entity.pk.LongEntity;

/**
 * 消息，消息包括用户发送消息，系统消息。用户之间可以相互发送消息
 * 消息标题最长为150个字符
 * 消息内容最长为2000个以内字符
 * @author slaton
 *
 */
@Entity
//表名与类名不相同时重新定义表名.
@Table(name = "TBL_MESSAGE")
//默认的缓存策略.
@SuppressWarnings("serial")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Message extends LongEntity{

	/**
	 * 消息内容
	 */
	private String content;
	
	/**
	 * 消息创建时间
	 */
	private Date ctime;
	
	/**
	 * 消息发送者ID
	 */
	private Long fromid;
	
	/**
	 * 消息类型
	 */
	private MessageType messageType;
	
	/**
	 * 是否已读
	 */
	private Boolean unread;
	
	/**
	 * 状态，ture:正常，false：删除
	 */
	private Boolean state;
	
	//标题
	private String title;
	
	//收件人id
	private List<User> toUsers;
	

	@Column(length=4000,name="CONTENT")
	public String getContent() {
		return content;
	}
	
	@Column(name="CTIME")
	public Date getCtime() {
		return ctime;
	}

	@Column(name="FROMID")
	public Long getFromid() {
		return fromid;
	}

	@Column(name="MESSAGE_TYPE")
	@Enumerated(EnumType.STRING)
	public MessageType getMessageType() {
		return messageType;
	}

	@Column(name="UNREAD")
	public Boolean getUnead() {
		return unread;
	}

	@Column(name="STATE")
	public Boolean getState() {
		return state;
	}

	@Column(name="TITLE",length=300)
	public String getTitle() {
		return title;
	}
	
	// 多对多定义
	@ManyToMany
	// 中间表定义,表名采用默认命名规则
	@JoinTable(name = "TBL_USER_MESSAGE", joinColumns = { @JoinColumn(name = "MID") }, inverseJoinColumns = { @JoinColumn(name = "UID") })
	// Fecth策略定义
	@Fetch(FetchMode.SUBSELECT)
	// 集合按id排序.
	@OrderBy("id")
	// 集合中对象id的缓存.
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<User> getToUsers() {
		return toUsers;
	}

	public void setContent(String content) {
		this.content = content;
	}



	public void setCtime(Date ctime) {
		this.ctime = ctime;
	}



	public void setFromid(Long fromid) {
		this.fromid = fromid;
	}



	public void setMessageType(MessageType messageType) {
		this.messageType = messageType;
	}



	public void setUnead(Boolean unread) {
		this.unread = unread;
	}



	public void setState(Boolean state) {
		this.state = state;
	}



	public void setTitle(String title) {
		this.title = title;
	}

	
	public void setToUsers(List<User> toUsers) {
		this.toUsers = toUsers;
	}


	public enum MessageType{
		
		COMMON,SYSTEM;
		
		MessageType(){
			
		}
		
		public String getLabel(){
			switch (this) {
			case SYSTEM:
				return "系统消息";
			case COMMON:
				return "普通消息";
			}
			return "未知";
		}
	}
}
